package com.xueyu.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xueyu.dao.RelativeDao;
import com.xueyu.dao.personinfoDao;
import com.xueyu.domain.Relative.RelativeBean;
import com.xueyu.domain.personinfo.personinfoBean;
import com.xueyu.util.ResultBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/relative")
public class RelativeController {
    @Autowired
    RelativeDao relativeDao;
    @Autowired
    personinfoDao personinfoDao;

    @RequestMapping(value = "/toAdd")
    public Map toAdd(){
        Map map = new HashMap();
        List<personinfoBean> personinfoBeanList = personinfoDao.selectList(null);
        map.put("personinfo",personinfoBeanList);
        return map;
    }

    @RequestMapping("/add")
    public ResultBean add(@RequestBody RelativeBean relative)throws Exception{
        if(relative.getRelativeId()==0){
            System.out.println(relative.getRelativeId()+"1111111");
            relativeDao.insert(relative);
        }else {
            relativeDao.updateById(relative);
        }
        return new ResultBean();
    }
    @RequestMapping("/list")
    public ResultBean list(@RequestBody Map map){
        int current = (Integer)map.get("Current");
        int size = (Integer)map.get("size");
        Page<RelativeBean> page = new Page<RelativeBean>(current,size);
        List<RelativeBean> relativeList = relativeDao.listPage(page,map);
        page.setRecords(relativeList);


        List<personinfoBean> personinfoList = personinfoDao.selectList(null);
        ResultBean resultBean = new ResultBean();
        resultBean.put("page",page);
        resultBean.put("personinfoList",personinfoList);
        System.out.println(relativeList);
        return resultBean;
    }

    @RequestMapping("/relativeById/{relativeId}")
    public ResultBean relativeById(@PathVariable("relativeId") int relativeId){
        ResultBean resultBean = new ResultBean();
        QueryWrapper<personinfoBean> queryWrapper = new QueryWrapper<personinfoBean>();
        Map map = new HashMap();
        if(relativeId > 0){
            RelativeBean relative = relativeDao.selectById(relativeId);

            queryWrapper.eq("personId",relative.getPersonId());
            List<personinfoBean> personinfoList = personinfoDao.selectList(queryWrapper);

            map.put("personify",personinfoList);
            map.put("relative",relative);
        }
        resultBean.put("data",map);
        return resultBean;
    }
    @RequestMapping("/del/{relativeId}")
    public ResultBean del(@PathVariable("relativeId") String relativeId){
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.in("relativeId",relativeId.split(","));
        relativeDao.delete(queryWrapper);
        return new ResultBean();
    }

}